<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//

namespace ChannelThrift\Service;

use Base\BaseService;


/**
 * 地址功能类
 * @author       Eric
 */
class AddressService extends BaseService{

    private $model = null;
    public  $connection = 'DB_AUMS';
    public $tablePrefix = 'au_';

    public function __construct() {
        $this->model = D('ChannelThrift/Aums/AddrArea');
    }

    /**
     * 获得某级别地址信息
     * @param int $level 级别信息
     * @param int $is_gat 是否包含港澳台 0 默认包含 1 不包含
     * @return array 地址信息数组
     */
    public function get_by_level($level, $is_gat = 0) {
        $m = $this->model;
        if (!is_numeric($level))
            return false;
        $condition['level'] = array('eq', $level);
        if ($is_gat) {
            $condition['city_code'] = array('NOT IN', '710000,810000,820000');
        }
        $level_add_info = $m->where($condition)->select();
        return $level_add_info;
    }

    /**
     * 获得子级别地址信息
     * @param int $pid 父地址信息
     * @return array 地址信息
     */
    public function get_by_pid($pid,$is_gat = 0) {
        $m = $this->model;
        if (!is_numeric($pid))
            return false;
        $condition['pid'] = array('eq', $pid);
        if ($is_gat) {
            $condition['city_code'] = array('NOT IN', '710000,810000,820000');
        }
        $level_add_info = $m->where($condition)->select();
        return $level_add_info;
    }

    /*
     * 获得地址code
     * @param string $name  地址名
     * @return int  地址编号 
     */

    public function get_code_by_name($name) {
        $m = $this->model;
        if (empty($name))
            return false;
        $code = $m->field('city_code')->where('city_name=\'' . $name . '\' and level=2')->find();
        if (!$code)
            $code = $m->field('city_code')->where('city_name=\'' . $name . '\' and level=1')->find();
        if (!$code)
            $code = $m->field('city_code')->where('city_name=\'' . $name . '\' and level=0')->find();
        if ($code)
            return $code['city_code'];
        else
            return false;
    }

    /**
     * 获得地址名
     * @param unknown $code
     * @return string 地址名
     */
    public function get_name_by_code($code) {
        $m = $this->model;
        if (!($code))
            return false;
        $code = $m->field('city_name')->where('city_code=\'' . $code . '\'')->find();
        if ($code)
            return $code['city_name'];
        else
            return false;
    }

    /**
     * 获取省份信息
     * @param int $city_code 地区编号
     */
    public function get_province($city_code) {
        $m = $this->model;
        return $m->alias('a')->field('a.city_name,b.city_name as province')
                        ->join(C('DB_PREFIX') . "addr_area b ON a.pid=b.city_code")
                        ->where("a.city_code='" . $city_code . "'")->find();
    }

    /**
     * 获取省份code
     * @param int $city_code 地区编号
     */
    public function get_province_code($city_code) {
        $m = $this->model;
        return $m->where(array('city_code' => $city_code))->getField('pid');
    }

    /**
     * 通过区id获取全部地址
     * @param int $area_id
     * @return array
     */
    public function get_full_name($area_id) {
        $map = array();
        $map['city_code'] = array('eq', $area_id);
        $res = M('addr_area')->field('full_name')->where($map)->find();
        if ($res)
            return $res['full_name'];
        else
            return false;
    }

    /**
     * 获取开展业务的城市列表
     * @param array $map 检索条件
     */
    public static function get_apply_city($map = array())
    {
        $arr = array();
        $rs = M('apply_city')->where($map)->select();
        foreach ($rs as $k => $v) {
            $arr[$v['code']] = $v['name'];
        }
        return $arr;
    }


}

?>